import 'package:casdon/core/logger/log.dart';
import 'package:casdon/core/network/business_exception.dart';
import 'package:casdon/modules/lifeCircle/data/api/life_topic_list_api.dart';
import 'package:casdon/modules/lifeCircle/data/repository/life_repository.dart';

class LifeTopicListUsecase {
  final LifeRepository _repository;

  const LifeTopicListUsecase(this._repository);

  // 特殊 subjectId 常量
  static const String followSubjectId = '__follow__';
  static const String recommendSubjectId = '__recommend__';

  Future<ResponseLifeTopicListApi> execute({
    required String id,
    required int pageNum,
    required int pageSize,
  }) async {
    try {
      ResponseLifeTopicListApi? ret;

      // 根据 subjectId 判断使用哪个接口
      if (id == followSubjectId) {
        // 关注列表
        ret = await _repository.getTopicFlowList(pageNum, pageSize);
      } else if (id == recommendSubjectId) {
        // 推荐列表
        ret = await _repository.getTopicRecomendList(pageNum, pageSize);
      } else {
        // 普通标签列表
        ret = await _repository.getTopicSubjectList(id, pageNum, pageSize);
      }

      ret ??= ResponseLifeTopicListApi(0, []);
      return ret;
    } on BusinessException {
      rethrow;
    } catch (e) {
      Log.e("LifeTopicListUsecase 失败", e.toString(), StackTrace.current);
      rethrow;
    }
  }
}
